
/**
 * 写cookies
 * @param {[String]}
 * @param {[String]}
 */
function setCookie(name, value, days) {
	var Days = days || 30; //此 cookie 将被保存 30 天
	var exp = new Date(); //new Date("December 31, 9998");
	exp.setTime(exp.getTime() + 60 * 60 * 1000 *24*30); 
	document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString() + "; path=/";
}; 
/**
 * 清除指定cookie 
 * @param  {[String]}
 */
function clearCookie(name) {  
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getCookie(name);
    if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString()+"; path=/";
} ; 
/**
 * 获取cookie
 * @param  {[String]}
 */
function getCookie(name) {  
	var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
	if (arr != null) return unescape(arr[2]);
	return null;
}  
/**
 * 清除所有cookie
 */
function clearAllCookie(){ 
	var cookies = document.cookie.split(";");
	for (var i = 0; i < cookies.length; i++) {
		var cookie = cookies[i];
		var eqPos = cookie.indexOf("=");
		var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
		document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/";
	}
	if (cookies.length > 0) {
		for (var i = 0; i < cookies.length; i++) {
			var cookie = cookies[i];
			var eqPos = cookie.indexOf("=");
			var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
			var domain = location.host.substr(location.host.indexOf('.'));
			document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; domain=" + domain;
		}
	}
} 


/**
 * 检测是否是微信浏览器
 * @return {Boolean}
 */
function isWeiXin () {
	let UA = navigator.userAgent;
	if ( UA.match(/MicroMessenger/i) == 'MicroMessenger' ) {
		return true;
	}
	return false;
}

/**
 * 获取URL参数
 * @param {[String]}
 */
function GetQueryString(name){
	var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if(r!=null)return  unescape(r[2]); return null;
}
//let appid='wx6bdc50e8150c7beb';//测试
 let appid='wxf71262a112685c25';//正式
/**
 * 微信授权地址跳转
 */
function wXAuthJump(urls) {
	// let urls=window.location.href;
	// if(GetQueryString('code')){
	// 	let urlss=urls.split('?');
	// 	let urlsss=urlss[1].split('#');
	// 	urls=urlss[0]+'?#'+urlsss[1];
	// }
	let sendData = {
		RETURN_URL:urls
	}
	this.$fetchPost('/UserWeb/authorization.action',
			sendData
		)
		.then(res=> {
			location.href=res.RETURN_URL;
		},err=> {
		})
}
/**
 * 发送unionid或code
 */
function sendWXCodeOrOpenid() {
	//console.warn(this)
	let that = this;
	let searchCode = GetQueryString('code');
	let userid=getCookie('loginkey');
	let mobile=getCookie('userName');
	let unionid=getCookie('_unionidpo_');
	if(unionid){
		searchCode='';
	}else{
		unionid='';
	}
	let sendData = {
		CODE: searchCode,
		MOBILE: mobile===undefined?'':mobile,
		USER_ID:userid===undefined?'':userid,
		UNIONID:unionid
	}
	// alert(JSON.stringify(sendData));
	// if ( searchCode !== '' && searchCode !== null && searchCode !== undefined ) {}
	return new Promise(function(resolve, reject) {
		// alert(searchCode)
		that.$fetchPost('/UserWeb/authorization.action',
			sendData
		)
		.then(res=> {
			let data = res;
			// 成功
			resolve(data);
		},err=> {
			reject(err);
		})

	})

}
/**
 * [pushStateUrl description]
 * @return {[type]} [description]
 * 该函数主要是解决多支付目录的问题
 * 将不同的支付目录前加一个？，在不改变路由状态的同时改变
 * 将所有的支付目录设置到同一个支付目录下
 */
function pushStateUrl(){
	let url=window.location.href;
	var urls=url.split("#");
	if(urls[0].indexOf('?')>-1){
		return;
	}else{
		history.pushState(null,null,"?#"+urls[1]);
	}

	/*else if(urls[1].indexOf('?')>-1){//当路由中带查询参数跳转
		let urlss=urls[1].split('?');
		let endUrl=urlss[1]+'#'+urlss[0];
		history.pushState(null,null,"?"+endUrl);
	}*/
}
function reloads(){
  if(GetQueryString('from')){
    let link=location.href.split('?');
    window.location.href =link[0]+'?time='+((new Date()).getTime())+"&"+link[1];
  }else{
  	location.reload();
  }
}
function iosAdroidWx(){
	var u = navigator.userAgent;
	var isAndroids = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; // android终端
	var isiOSs = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); // ios终端
	var urls='';
	var appid='';
	if(isAndroids){
		urls='/OAuth/getAccess_token.action';
		appid='wxf71262a112685c25';
	}else if(isiOSs){
		urls='/OAuth/getAccess_tokenIPHONE.action';
		appid='wx054c6f61cacfa911';
	}
	return {
		urls:urls,
		appid:appid
	}
}
export {
	// cookie相关
	setCookie,
    clearCookie,
    getCookie,
    clearAllCookie,
    // 微信相关
	isWeiXin,
	// 其他相关
	GetQueryString,
	pushStateUrl,
	appid,
	reloads,
	wXAuthJump,
	sendWXCodeOrOpenid,
	iosAdroidWx
}